//
//  AIChooseSoulmateView.m
//  MiaMind
//
//  Created by Vill on 2023/12/11.
//

#import "AIChooseSoulmateView.h"
#import "AIChooseCollectionCell.h"

@interface AIChooseSoulmateView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UIButton *backBtn;   //返回按钮
@property (nonatomic, strong) UILabel *titleLbl;   //标题
@property (nonatomic, strong) UIButton *nextBtn;   //下一步
@property (nonatomic, strong) UICollectionView *colloctionView; //collectionview
@property (nonatomic, strong) NSMutableArray *soulMateArr;

@end

@implementation AIChooseSoulmateView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]){
        self.backgroundColor = UIColor.clearColor;
        [self setUI];
        // 获取灵魂伴侣列表
        [self loadAiList];
    }
    return self;
}

-(void)setUI{
    [self addSubview:self.backBtn];
    [self addSubview:self.titleLbl];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(34));
        make.leading.mas_equalTo(self).mas_offset(PTW(13));
        make.top.mas_equalTo(self).mas_offset(KStatusBarHeight);
    }];
    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self.backBtn);
    }];
    [self addSubview:self.nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(PTW(170), PTW(84)));
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(self).mas_offset(-KBottomSafeHeight);
    }];
    [self addSubview:self.colloctionView];
    [self.colloctionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self);
        make.bottom.mas_equalTo(self.nextBtn.mas_top).mas_offset(-PTW(30));
        make.height.mas_equalTo(PTW(85));
    }];
}

// 获取ai列表数据
-(void)loadAiList{
    [MBProgressTool mb_showHUDInView:AIUnityInstance.instance.unityAppController.rootView];
    [AIManager requestMethod:@"GET" url:api_List001 parameters:@{} successBlock:^(id  _Nullable result) {
        NSLog(@"result ------ %@",result);
        [MBProgressTool mb_hideHUDInView:AIUnityInstance.instance.unityAppController.rootView];
        [self.soulMateArr removeAllObjects];
        self.soulMateArr = [AISoulMateModel mj_objectArrayWithKeyValuesArray:result];
        [self.colloctionView reloadData];
        if (self.soulMateArr.count > 0){
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self collectionView:self.colloctionView didSelectItemAtIndexPath:indexPath];
            [self.colloctionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        }
    } failedBlock:^(NSError * _Nullable error) {
        [MBProgressTool mb_hideHUDInView:AIUnityInstance.instance.unityAppController.rootView];
    } showHUD:NO];
}

#pragma mark --------------- lazy -----------
- (NSMutableArray *)soulMateArr{
    if(!_soulMateArr){
        _soulMateArr = [NSMutableArray array];
    }
    return _soulMateArr;
}

- (UIButton *)backBtn{
    if(!_backBtn){
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_backBtn setImage:KImaged(@"ai_base_back") forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UILabel *)titleLbl{
    if(!_titleLbl){
        _titleLbl = [UILabel new];
        _titleLbl.text = @"Choose your AI soulmate";
        _titleLbl.font = [UIFont fontWithWeight:MTBold size:PTW(15)];
        _titleLbl.textColor = UIColor.whiteColor;
        _titleLbl.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLbl;
}

- (UIButton *)nextBtn{
    if(!_nextBtn){
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextBtn setBackgroundImage:KImaged(@"ai_regis_next") forState:UIControlStateNormal];
        [_nextBtn setTitle:@"Next" forState:UIControlStateNormal];
        [_nextBtn setTitleColor:[UIColor colorHex:@"#5C5C5C"] forState:UIControlStateNormal];
        _nextBtn.titleLabel.font = [UIFont fontWithWeight:MTBold size:PTW(15)];
        [_nextBtn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}

- (UICollectionView *)colloctionView{
    if (!_colloctionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(0, PTW(10), 0, PTW(10));
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = PTW(10);
        layout.minimumInteritemSpacing = PTW(5);
        layout.itemSize = CGSizeMake(PTW(83), PTW(83));
        _colloctionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _colloctionView.backgroundColor = [UIColor clearColor];
        _colloctionView.showsHorizontalScrollIndicator = NO;
        _colloctionView.dataSource = self;
        _colloctionView.delegate = self;
        [_colloctionView registerClass:[AIChooseCollectionCell class] forCellWithReuseIdentifier:@"AIChooseCollectionCell"];
        if (@available(iOS 11.0, *)) {
            _colloctionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _colloctionView;
}

// 返回按钮
-(void)backAction{}

// next按钮
-(void)nextAction:(UIButton *)sender{
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (self.nextClickBlock){
            self.nextClickBlock();
        }
    }];
}

#pragma mark ------------- UICollectionViewDelegate,UICollectionViewDataSource  ---------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.soulMateArr.count;
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AIChooseCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AIChooseCollectionCell" forIndexPath:indexPath];
    if (self.soulMateArr.count > 0){
        AISoulMateModel *model = self.soulMateArr[indexPath.item];
        cell.soulMateModel  = model;
    }
    return cell;
}

// 选中
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"indexpath -------- %ld",(long)indexPath.item);
    AISoulMateModel *model = self.soulMateArr[indexPath.item];
    // 是否需要VIP
    AIUserInfoModel *userInfo = [AIUserInfoManager shareManager].userInfo;
    // pro:需要VIP
    if (model.pro && !userInfo.vip){
        // 取消当前选择
        [collectionView deselectItemAtIndexPath:indexPath animated:YES];
        // 选中上次的选择
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
        // 充值vip
        if (self.needVipBlock){
            self.needVipBlock(self.soulMateArr[0],0);
        }
    }else{
        if (self.itemClickBlock){
            self.itemClickBlock(model,indexPath.item);
        }
    }
}

@end
